package com.example.demo.controller;

 import com.example.demo.entity.Supplier;
 import com.example.demo.service.SupplierService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
 import java.util.List;

@RestController
@RequestMapping("suppliers")
public class SupplierController  {

    @Autowired
    SupplierService supplierService;

    @GetMapping("/{id}")
    public ResponseEntity<Supplier> getSupplier(@PathVariable Long id){
         Supplier result=supplierService.getSupplier(id);
        if(result==null){
            return ResponseEntity.noContent().build();
        }else {
            return ResponseEntity.ok(result);
        }
    }

    @GetMapping("")
    public ResponseEntity<List<Supplier>> findSupplier(int id , String name, String phoneNumber){
        List<Supplier> result=supplierService.findSupplier(id,name,phoneNumber);
        return ResponseEntity.ok(result);
    }

    @PostMapping("")
    public ResponseEntity<Supplier> addSupplier(@RequestBody Supplier supplier){
        Supplier result=supplierService.addSupplier((supplier));
        return ResponseEntity.ok(result);
    }
    @PutMapping("/{id}")
    public ResponseEntity<Void> updateSupplier(@PathVariable Long id,@RequestBody Supplier supplier){
        supplierService.updateSupplier(id,supplier);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteSupplier(@PathVariable Long id){
        supplierService.deleteSupplier(id);
        return ResponseEntity.ok().build();
    }
}
